//
//  DeveloperView.swift
//  Social
//
//  Created by Hayden on 2019/7/12.
//  Copyright © 2019 shengsheng. All rights reserved.
//

import UIKit
import SystemConfiguration.CaptiveNetwork

class DeveloperView: UIView {
    
    var state: State = .main {
        didSet {
            setColor(tint: state.tintColor, text: state.textColor)
            apiField.text = Env(rawValue: state.rawValue)!.commonHost
            configField.text = Env(rawValue: state.rawValue)!.configHost
            modeControl.selectedSegmentIndex = state.rawValue - 1
            apiField.isEnabled = state == .mock
            configField.isEnabled = state == .mock
        }
    }

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var modeControl: UISegmentedControl!
    @IBOutlet weak var apiField: UITextField!
    @IBOutlet weak var configField: UITextField!
    @IBOutlet weak var versionLabel: UILabel!
    @IBOutlet weak var networkLabel: UILabel!
    @IBOutlet weak var commitButton: UIButton!
    @IBOutlet weak var urlSwitch: UISwitch!
    @IBOutlet weak var reportButton: UIButton!
    @IBOutlet weak var copyLogButton: UIButton!
    
    override func awakeFromNib() {
        setupAppearance()
        addObservers()
    }
    
    deinit {
        removeObservers()
    }
    
    fileprivate func setupAppearance() {
        modeControl.borderWidth = 1
        modeControl.borderColor = UIColor.blue
        modeControl.cornerRadius = modeControl.bounds.height / 2
        state = State(rawValue: Env.current.rawValue) ?? .mock
        urlSwitch.cornerRadius = 15.5
        let appVersion = UIApplication.shared.semanticVersion ?? "0.0.0"
        let appBuildNumber = UIApplication.shared.buildNumber ?? "0"
        let networkSSID = UIDevice.current.ssid ?? "unknown"
        let networkBSSID = UIDevice.current.bssid ?? "unknown"
        versionLabel.text = "Version: \(appVersion)  Build: \(appBuildNumber)"
        networkLabel.text = "AP: \(networkSSID) (\(networkBSSID))"
    }
    
    @IBAction func modeSwitched(_ sender: UISegmentedControl) {
        state = State(rawValue: sender.selectedSegmentIndex + 1) ?? .mock
    }
    
    private func setColor(tint tintColor: UIColor, text textColor: UIColor) {
        UIView.animate(withDuration: 0.2) {
            self.backgroundColor = tintColor
            self.modeControl.tintColor = tintColor
            self.modeControl.borderColor = tintColor
            self.commitButton.backgroundColor = tintColor
            self.urlSwitch.onTintColor = tintColor
            self.apiField.textColor = textColor
            self.configField.textColor = textColor
            self.reportButton.backgroundColor = tintColor
            self.copyLogButton.backgroundColor = tintColor
        }
    }
    
    private func addObservers() {
        NotificationCenter.default.addObserver(self,
            selector: #selector(keyboardWillShow),
            name: UIResponder.keyboardWillShowNotification,
            object: nil
        )
        NotificationCenter.default.addObserver(self,
            selector: #selector(keyboardDidHide),
            name: UIResponder.keyboardDidHideNotification,
            object: nil
        )
    }
    
    private func removeObservers() {
        NotificationCenter.default.removeObserver(self,
            name: UIResponder.keyboardWillShowNotification,
            object: nil
        )
        NotificationCenter.default.removeObserver(self,
            name: UIResponder.keyboardDidHideNotification,
            object: nil
        )
    }
    
    @objc func keyboardWillShow() {
        scrollView.isScrollEnabled = false
    }
    
    @objc func keyboardDidHide() {
        scrollView.isScrollEnabled = true
    }

}

extension DeveloperView {
    
    enum State: Int {
        case main = 1, beta, code, mock
        
        var tintColor: UIColor {
            switch self {
            case .main: return UIColor(hex: 0x43A047)
            case .beta: return UIColor(hex: 0x1E88E5)
            case .code: return UIColor(hex: 0xFB8C00)
            case .mock: return UIColor(hex: 0xE53935)
            }
        }
        
        var textColor: UIColor {
            switch self {
            case .main: return UIColor(hex: 0x2E7D32)
            case .beta: return UIColor(hex: 0x1565C0)
            case .code: return UIColor(hex: 0xEF6C00)
            case .mock: return UIColor(hex: 0xC62828)
            }
        }
    }
    
}

